<template>
  <div class="app-container">
    <el-row style="margin-bottom:10px;">
      <el-input placeholder="企业ID" style="margin-left:5px;float:left;width:100px;" />
      <el-input placeholder="企业名称" style="margin-left:5px;float:left;width:150px;" />
      <el-select v-model="search_type" placeholder="企业类型" style="margin-left:5px;float:left;width:120px;">
        <el-option label="屠宰企业" value="1" />
        <el-option label="批发市场" value="2" />
        <el-option label="农贸市场" value="3" />
      </el-select>
      <el-input placeholder="企业地址" style="margin-left:5px;float:left;width:200px;" />
      <el-select v-model="search_checkstatus" placeholder="审核状态" style="margin-left:5px;float:left;width:120px;">
        <el-option label="已审核" value="1" />
        <el-option label="未审核" value="2" />
      </el-select>
      <el-button type="primary" icon="el-icon-search" style="margin-left:5px;float:left;width:100px;">搜索</el-button>
      <el-button
        type="primary"
        style="float:right"
        @click="newRow"
      >新增一个企业</el-button>
    </el-row>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      fit
      highlight-current-row
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="25"
      />
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="名称" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="类型" show-overflow-tooltip width="100">
        <template slot-scope="scope">
          {{ scope.row.type }}
        </template>
      </el-table-column>
      <el-table-column label="地址" width="110" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.address }}</span>
        </template>
      </el-table-column>
      <el-table-column label="管理者" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.manager_id }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="登记状态" width="110" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.examine | statusFilter">{{ scope.row.examine === 1 ? '通过' : '未审批' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建日期" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.create_date }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" @click="editRow(scope.row)">编辑</el-button>
          <el-button :loading="buttonloading" type="danger" @click="deleteRow(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="margin:10px;">
      <span style="margin-right:5px;">选中 {{ multipleSelection.length }} 项 批量操作</span>
      <el-button :loading="buttonloading" type="danger" @click="deleteRow(multipleSelection)">删除</el-button>
      <el-button :loading="buttonloading" type="success" @click="onSetPass(multipleSelection)">置审批</el-button>
      <el-button :loading="buttonloading" type="info" @click="onSetUnPass(multipleSelection)">置未审批</el-button>
    </div>
    <div class="block" style="text-align:center;">
      <el-pagination
        :current-page.sync="currentPage"
        :page-sizes="[10, 15, 30, 50]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>
  </div>
</template>

<script>
import { deleteData, getList, setPass } from '@/api/company'
import moment from 'moment'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      search_type: '',
      search_checkstatus: '',
      buttonloading: false,
      currentPage: 1,
      total: 0,
      pagesize: 15,
      list: null,
      listLoading: true,
      multipleSelection: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    fetchData() {
      this.listLoading = true
      getList({ current: this.currentPage, size: this.pagesize }).then(response => {
        this.list = response.obj.records
        this.normalizeData()
        this.total = response.obj.total
        this.listLoading = false
      }).catch(() => { this.listLoading = false })
    },
    normalizeData() {
      this.list.forEach((x) => {
        x.create_date = moment(x.create_date).format('YYYY-MM-DD HH:mm:ss')
        switch (x.type) {
          case 1:
            x.type = '屠宰企业'
            break
          case 2:
            x.type = '批发市场'
            break
          case 3:
            x.type = '农贸市场'
            break
        }
      })
    },
    editRow(row) {
      // console.log('employeeManage/companyEdit/' + row.id)
      this.$router.push({ path: '/enterpriseManage/companyEdit', query: { eid: row.id, type: 'edit' }})
    },
    newRow() {
      this.$router.push({ path: '/enterpriseManage/companyEdit', query: { type: 'new' }})
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.handleCurrentChange(val)
    },
    handleCurrentChange(val) {
      this.list = null
      this.listLoading = true
      getList({ current: this.currentPage, size: this.pagesize }).then(response => {
        this.list = response.obj.records
        this.normalizeData()
        this.total = response.obj.total
        this.listLoading = false
      }).catch(() => { this.listLoading = false })
    },
    deleteRow(row) {
      if (!(row instanceof Array)) {
        row = [row]
      }
      if (row.length === 0) {
        this.$message({
          type: 'info',
          message: '请至少选中一项'
        })
        return
      }
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.buttonloading = true
        deleteData(row).then((response) => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        })
        for (let i = 0; i < this.list.length; i++) {
          for (let j = 0; j < row.length; j++) {
            if (this.list[i].id === row[j].id) {
              this.list.splice(i, 1)
              i--
              row.splice(j, 1)
              j--
              break
            }
          }
        }
        this.buttonloading = false
      }).catch((reason) => {
        console.log(reason)
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
        this.buttonloading = false
      })
    },
    onSetPass(value) {
      if (value.length === 0) {
        this.$message({
          type: 'info',
          message: '请至少选中一项'
        })
        return
      }
      this.buttonloading = true
      setPass({ obj: value, examine: 1 }).then((response) => {
        this.$message({
          type: 'success',
          message: '操作成功'
        })
        for (let i = 0; i < this.list.length; i++) {
          for (let j = 0; j < value.length; j++) {
            if (this.list[i].id === value[j].id) {
              this.list[i].examine = 1
              break
            }
          }
        }
        this.buttonloading = false
      }).catch(() => { this.buttonloading = false })
    },
    onSetUnPass(value) {
      if (value.length === 0) {
        this.$message({
          type: 'info',
          message: '请至少选中一项'
        })
        return
      }
      this.buttonloading = true
      setPass({ obj: value, examine: 0 }).then((response) => {
        this.$message({
          type: 'success',
          message: '操作成功'
        })
        for (let i = 0; i < this.list.length; i++) {
          for (let j = 0; j < value.length; j++) {
            if (this.list[i].id === value[j].id) {
              this.list[i].examine = 0
              break
            }
          }
        }
        this.buttonloading = false
      }).catch(() => { this.buttonloading = false })
    }
  }
}
</script>
